package com.gomore.opple.module.gooddetail;

import com.gomore.opple.R;
import com.gomore.opple.data.DataRepository;
import com.gomore.opple.data.remote.retrofit.HttpResponseFunc;
import com.gomore.opple.data.remote.retrofit.RxSubscriber;
import com.gomore.opple.data.remote.retrofit.ServerResponseFunc;
import com.gomore.opple.exception.ApiException;
import com.gomore.opple.model.EventQueryShoppingCar;
import com.gomore.opple.rest.shopcart.SaveShoppingCartRequest;
import com.gomore.opple.web.cgform.consumer.entity.TOConsumerEntity;
import com.gomore.opple.web.cgform.goods.entity.TOGoodsEntity;

import javax.inject.Inject;

import de.greenrobot.event.EventBus;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

public class GoodDetailPresenter implements GoodDetailContract.Presenter {

    private DataRepository mDataRepositroy;
    private final GoodDetailContract.View mView;
    private CompositeSubscription mSubscriptions;
    private TOGoodsEntity toGoodEntity;

    @Inject
    GoodDetailPresenter(DataRepository dataRepository, GoodDetailContract.View view) {
        mDataRepositroy = dataRepository;
        mView = view;
        mSubscriptions = new CompositeSubscription();
        mView.setPresenter(this);
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        mSubscriptions.clear();
    }

    /**
     * 初始化数据
     */
    @Override
    public void prepareInitData() {

    }

    @Override
    public void getGoodById(String goodId) {
        unsubscribe();
        mView.showProgressDialog();
        Subscription subscription = mDataRepositroy.getGoodById(goodId)
                .map(new ServerResponseFunc<TOGoodsEntity>())
                .onErrorResumeNext(new HttpResponseFunc<TOGoodsEntity>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<TOGoodsEntity>() {
                    @Override
                    public void onNext(TOGoodsEntity toGoodsEntity) {
                        super.onNext(toGoodsEntity);
                        mView.hideProgressDialog();
                        toGoodEntity = toGoodsEntity;
                        mView.showContentView(toGoodsEntity);
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                    }
                });
        mSubscriptions.add(subscription);
    }

    @Override
    public void addToShoppingCar(SaveShoppingCartRequest saveShoppingCartRequest) {
        unsubscribe();
        mView.showProgressDialog();
        Subscription subscription = mDataRepositroy.addToShoppingCar(saveShoppingCartRequest)
                .map(new ServerResponseFunc<String>())
                .onErrorResumeNext(new HttpResponseFunc<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<String>() {
                    @Override
                    public void onNext(String string) {
                        super.onNext(string);
                        mView.hideProgressDialog();
                        mView.showMessage(R.string.add_success);
                        mView.finish();
                        EventQueryShoppingCar eventQueryShoppingCar = new EventQueryShoppingCar();
                        eventQueryShoppingCar.setIsGetShoppingCar(true);
                        EventBus.getDefault().post(eventQueryShoppingCar);
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                    }
                });
        mSubscriptions.add(subscription);
    }

    @Override
    public TOConsumerEntity getConsumer() {
        return mDataRepositroy.getConsumer();
    }

    @Override
    public TOGoodsEntity getTOGoodsEntity() {
        return toGoodEntity;
    }

}
